package com.insta.library;

import java.text.ParseException;

import com.insta.library.model.Author;
import com.insta.library.model.Book;
import com.insta.library.model.Editor;
import com.insta.library.model.LiteraryGenre;
import com.thoughtworks.xstream.XStream;

/**
 * Hello world!
 * 
 */
public class Xstream {
	private static XStream xstream;
	private Object objectName;
	private String xml;

	public Xstream(Object object) {
		xstream = new XStream();
		this.objectName = object;
		this.mapping(object);
	}

	public static void main(String[] args) throws ParseException {
		
		Book b = new Book();
		b.setReference("ME0008XFTG");
		b.setResume("Un homme et un seul a découvert la vérité... Steve Jobs is back !");
		b.setTitle("Apple Life");
		LiteraryGenre lg = new LiteraryGenre("Fiction","...");
		b.setGenre(lg);
		Author au = new Author("Jobs", "Steve", "Américaine", null, null);
		b.setAuthor(au);
		Editor ed = new Editor("ENI", "097643236899", null, null, 75000, "Paris", "Française");
		b.setEditor(ed);
		
		Xstream x = new Xstream(b);
		x.mapping(lg);
		x.mapping(au);
		x.mapping(ed);
		x.generateXML();
		System.out.println(x.getXml());
		
	}

	public void generateXML() {
		this.xml = xstream.toXML(this.objectName);
	}

	public void convertToObject(String xml) {
		this.objectName = (Object) xstream.fromXML(xml);
	}

	public void mapping(Object o) {
		xstream.alias(o.getClass().getSimpleName().toLowerCase(),
				o.getClass());
	}

	public static XStream getXstream() {
		return xstream;
	}

	public static void setXstream(XStream xstream) {
		Xstream.xstream = xstream;
	}

	public Object getObjectName() {
		return this.objectName;
	}

	public void setObjectName(Object objectName) {
		this.objectName = objectName;
	}

	public String getXml() {
		return this.xml;
	}

	public void setXml(String xml) {
		this.xml = xml;
	}
}
